﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using GamePlugin.Logics.Data;
using System.Diagnostics;
using GamePlugin.Logics.Game;
using GamePlugin.GGraphics;
using WindowsInput;
using GamePlugin.Logics.Game.Actions;
using System.IO;
using GamePlugin.IO;
using System.Windows.Media;
using System.Drawing;
using System.Windows.Media.Imaging;
using System.Drawing.Imaging;

namespace GamePlugin.Logics.Core
{
    public static class Helper
    {
        public static Random Rand = new Random();

        public static int GetFirstValueIndex(SearchItem item)
        {
            for (int i = 0; i < 6; i++)
                if (Helper.GetPropertyIndex(item, i) != 0)
                    return i;
            return -1;
        }

        public static float GetPropertyValue(SearchItem item, int index)
        {
            float ret = 0;
            switch (index)
            {
                case 0:
                    ret = item.Property1Value;
                    break;
                case 1:
                    ret = item.Property2Value;
                    break;
                case 2:
                    ret = item.Property3Value;
                    break;
                case 3:
                    ret = item.Property4Value;
                    break;
                case 4:
                    ret = item.Property5Value;
                    break;
                case 5:
                    ret = item.Property6Value;
                    break;
                default:
                    MessageBox.Show("Index is out of range in GetPropertyIndex()");
                    break;
            }

            return ret;
        }

        public static int GetFilterPropertyCount(SearchItem item, int index)
        {
            int count = Helper.GetMainWindow().searchInformation.PropertyBox1.Items.Count;

            for (int j = 0; j < ActionData.PropertyCount; j++)
            {
                if (GetPropertyIndex(item, j) != 0 && Workspace.Instance.SearchItemCompare.PropertyFilled[j] == true)
                    count--;
            }
            return count;
        }

        public static int GetFliterPropertyIndex(SearchItem item, int index)
        {
            int ret = 0;
            switch (index)
            {
                case 0:
                    ret = item.Property1;
                    break;
                case 1:
                    ret = item.Property2;
                    break;
                case 2:
                    ret = item.Property3;
                    break;
                case 3:
                    ret = item.Property4;
                    break;
                case 4:
                    ret = item.Property5;
                    break;
                case 5:
                    ret = item.Property6;
                    break;
                default:
                    MessageBox.Show("Index is out of range in GetPropertyIndex()");
                    break;
            }

            int count = 0;
            for (int j = 0; j < ActionData.PropertyCount; j++)
                if (GetPropertyIndex(item, j) != 0 && GetPropertyIndex(item, j) < ret
                    && Workspace.Instance.SearchItemCompare.PropertyFilled[j] == true)
                    count++;
            ret = ret - count;
            return ret;
        }

        private static int GetPropertyIndex(SearchItem item, int index)
        {
            switch (index)
            {
                case 0:
                    return item.Property1;
                case 1:
                    return item.Property2;
                case 2:
                    return item.Property3;
                case 3:
                    return item.Property4;
                case 4:
                    return item.Property5;
                case 5:
                    return item.Property6;
                default:
                    MessageBox.Show("Index is out of range in GetPropertyIndex()");
                    break;
            }
            return 0;
        }

        public static Bitmap GetImageInImage(Bitmap bitmap, Rectangle rect)
        {
            return bitmap.Clone(rect, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        }

        public static string GetPropertyStateName(int propertyIndex)
        {
            return typeof(DActionRunProperty).Name + propertyIndex.ToString();
        }
        public static void Sleep(int milliSeconds)
        {
            System.Threading.Thread.Sleep(milliSeconds);
        }

        public static void MoveCursor(System.Drawing.Point point)
        {
            MoveCursor((int)point.X, (int)point.Y);
        }

        public static void MoveCursor(int x, int y)
        {
            System.Drawing.Rectangle Resolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds;
            InputSimulator input = Workspace.Instance.DiabloPluginGame.InputSimulator;

            int xx = (int)(((float)x / Resolution.Width) * 65535.0f);
            int yy = (int)(((float)y / Resolution.Height) * 65535.0f);

            input.Mouse.MoveMouseTo(xx, yy);
        }

        public static System.Drawing.Point CursorPosition
        {
            get
            {
                System.Drawing.Point point = new System.Drawing.Point(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y);
                return point;
            }
        }
        public static ScreenChecker ScreenChecker
        {
            get
            {
                return Workspace.Instance.DiabloPluginGame.ScreenChecker;
            }
        }

        public static DiabloPluginGame DiabloPluginGame
        {
            get
            {
                return Workspace.Instance.DiabloPluginGame;
            }
        }

        public static string GetChinesePrice(int price)
        {
            const int Unit = 10000;
            string ret = "0";

            if (price > 0)
            {
                int a, b, c;
                a = price / Unit / Unit;
                b = price / Unit % Unit;
                c = price % Unit;
                ret = "";
                if (a > 0)
                    ret += a.ToString() + "亿";
                if (b > 0)
                    ret += b.ToString() + "万";
                if (c > 0)
                    ret += c.ToString();
            }

            ret += "金幣";
            return ret;
        }

        public static ImageSource GetImageSource(Bitmap bitmap)
        {
            using (MemoryStream memory = new MemoryStream())
            {
                bitmap.Save(memory, ImageFormat.Png);
                memory.Position = 0;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.BeginInit();
                bitmapImage.StreamSource = memory;
                bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
                bitmapImage.EndInit();
                return bitmapImage;
            }
            return null;
        }

        public static void LogState(string text)
        {
            Workspace.Instance.StateLogData.RunState = Helper.CursorPosition + " " + text;

            //Helper.Log(text);
        }

        public static void Log(string text, bool allowDuplicated = false)
        {
            if (allowDuplicated == true || allowDuplicated == false && Workspace.Instance.StateLogData.LastElement != text)
            {
                Workspace.Instance.StateLogData.Children.Add(text);
                if (Workspace.Instance.StateLogData.Children.Count > 100)
                    Workspace.Instance.StateLogData.Children.RemoveAt(0);

                IOManager.Instance.Log(text);
            }
        }

        public static MainWindow GetMainWindow()
        {
            return (Application.Current.MainWindow as MainWindow);
        }

        public static string GetLengendItemName(string name)
        {
            int start = name.IndexOf(SearchItem._ITEM_NAME_LEFT);
            int end = name.IndexOf(SearchItem._ITEM_NAME_RIGHT);
            if (start >= 0 && end >= 0)
            {
                string itemName = name.Substring(start, end - start + 1);
                return itemName;
            }
            return "";
        }

        public static List<string> GetList(string kind)
        {
            if (Workspace.Instance.PropertyItemList == null)
                return new List<string>();

            GamePlugin.Logics.Data.PropertyItem item = Workspace.Instance.PropertyItemList.GetPropertyItem(kind);
            if (item != null)
                return item.Children.ToList<string>();
            else
                return new List<string>();
        }

        public static SearchItem[] SearchItemArray =
            new SearchItem[] { new SearchItem(), new SearchItem(), new SearchItem(), new SearchItem() };
        public static int CurrentDepth = 0;

        public static void FillCombox(string kind, ComboBox box)
        {
            List<string> list = GetList(kind);

            bool reset = false;

            if (list.Count == box.Items.Count)
            {
                for (int i = 0; i < list.Count; i++)
                    if (list[i] != box.Items.GetItemAt(i))
                        reset = true;
            }
            else
                reset = true;

            if (reset)
            {
                box.Items.Clear();
                for (int i = 0; i < list.Count; i++)
                    box.Items.Add(list[i]);
                box.SelectedIndex = 0;
            }
        }

        public static int GetIndex(string kind, string name)
        {
            List<string> list = GetList(kind);
            return list.IndexOf(name);
        }

        public static string GetName(string kind, int index)
        {
            List<string> list = GetList(kind);
            if (index < 0 || index >= list.Count)
                return "";
            return list[index];
        }
    }
}
